import React from 'react';
import moment from 'moment';
import fx from '../utils/fx';
import dicts from '../dicts';

const colorTrader = (text) => {
  const t = dicts.tradeType[text];
  const color = 'Sell'=== text ? 'green' : 'red';
  return (<span style={{ color }}>{t}</span>);
};

const columns = [
  {
    title: '股票代码',
    dataIndex: 'symbol',
    sorter: true,
  },
  {
    title: '类型',
    dataIndex: 'tradeType',
    sorter: true,
    render: text => colorTrader(text),
  },
  {
    title: '买入价格 / 时间',
    dataIndex: 'buyPrice',
    sorter: true,
    render: (text, row) => (
      <span className="mergeCell">{fx.formatNumber(text, 2)}<br />
        <i>{moment(new Date(('Sell'=== row.tradeType ? row.closeTime : row.openTime))).format('YYYY-MM-DD HH:mm:ss')}</i>
      </span>),
  },
  {
    title: '卖出价格 / 时间',
    dataIndex: 'sellPrice',
    sorter: true,
    render: (text, row) => (
      <span className="mergeCell" >{fx.formatNumber(text, 2)}<br />
        <i>{moment(new Date(('Sell'=== row.tradeType ? row.openTime : row.closeTime))).format('YYYY-MM-DD HH:mm:ss')}</i>
      </span>),
  },
  {
    title: '交易数量',
    dataIndex: 'quantity',
    sorter: true,
  },
  {
    title: '费用',
    dataIndex: 'fee',
    render: text => fx.formatNumber(text, 2),
    sorter: true,
  },
  {
    title: '盈亏',
    dataIndex: 'yield',
    render: (text, row) => {
      const { tradeType, buyPrice, sellPrice, quantity, fee } = row;
      return fx.colorMoney((sellPrice - buyPrice) * quantity - fee);
    },
  },
];

export default columns;
